package cn.tradewin.advisor.persist.services.impl;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.slf4j.Logger;

import cn.tradewin.advisor.persist.mapper.TaskMapper;
import cn.tradewin.advisor.persist.model.CodeLabel;
import cn.tradewin.advisor.persist.model.Task;
import cn.tradewin.advisor.persist.services.MTaskService;

public class MTaskServiceImpl implements MTaskService {

private final SqlSessionFactory sqlSessionFactory;
	
	private final Logger log;

	public MTaskServiceImpl(SqlSessionFactory sqlSessionFactory, Logger log) {
		this.log = log;
		this.sqlSessionFactory = sqlSessionFactory;
	}

	@Override
	public List<Task> getAllTasks() {
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession();
			TaskMapper mapper = session.getMapper(TaskMapper.class);
			return mapper.selectAll();
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new RuntimeException(e.getMessage());
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}
	
	@Override
	public Task getTask(String taskId) {
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession();
			TaskMapper mapper = session.getMapper(TaskMapper.class);
			return mapper.selectByPrimaryKey(new Byte(taskId));
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new RuntimeException(e.getMessage());
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	@Override
	public int addTask(Task task) {
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession();
			TaskMapper mapper = session.getMapper(TaskMapper.class);
			int count = mapper.insert(task);
			if (count < 0) {
				throw new RuntimeException("failure add department [" + task.getId() + "]");
			}
			session.commit();
			return count;
		} catch (Exception e) {
			log.error(e.getMessage());
			if (session != null) {
				session.rollback();
			}
			throw new RuntimeException(e.getMessage());
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	@Override
	public int updTask(Task task) {
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession();
			TaskMapper mapper = session.getMapper(TaskMapper.class);
			int count = mapper.updateByPrimaryKey(task);
			if (count < 0) {
				throw new RuntimeException("update failure on department [" + task.getId() + "]");
			}
			session.commit();
			return count;
		} catch (Exception e) {
			log.error(e.getMessage());
			if (session != null) {
				session.rollback();
			}
			throw new RuntimeException(e.getMessage());
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	@Override
	public List<CodeLabel> getAllTasksOptions() {
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession();
			TaskMapper mapper = session.getMapper(TaskMapper.class);
			return mapper.getAllTasksOptions();
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new RuntimeException(e.getMessage());
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	@Override
	public List<Task> getTasksInProject(Integer projectId) {
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession();
			TaskMapper mapper = session.getMapper(TaskMapper.class);
			return mapper.selectTasksInProject(projectId);
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new RuntimeException(e.getMessage());
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	@Override
	public List<Task> getTasksNotInProject(Integer projectId) {
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession();
			TaskMapper mapper = session.getMapper(TaskMapper.class);
			return mapper.selectTasksNotInProject(projectId);
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new RuntimeException(e.getMessage());
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}
}